package ui;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;


import service.*;
import model.*;
import util.*;
import dao.*;




public class UserUI extends JFrame {
	private int uid;
	private RecordDao recordDao=RecordDao.getinstance();
	private RoomDao roomDao=RoomDao.getInstance();
	private RemindDao remindDao=RemindDao.getInstance();
	private UserDao userDao=UserDao.getInstance();
	private UserService userService=UserService.getInstance();
	
	private Toolkit toolkit=Toolkit.getDefaultToolkit();
	private Dimension sc=toolkit.getScreenSize();
	
	private JLabel card0=new JLabel();//用于JTabbedPane的标签
	private JLabel card1=new JLabel();
	private JLabel card2=new JLabel();
	private JPanel card3=new JPanel();
	private JPanel card4=new JPanel();
	
	private JLabel type1=new JLabel("房间类型");     //第一个选项卡
	private JTextField tType1=new JTextField();
	private JButton bRemind1=new JButton("预约");
	private JButton bQuery1 = new JButton("查 找");
	private JLabel rid1 = new JLabel("房间序号");
    private JLabel address1 = new JLabel("房间位置");
    private JLabel count1 = new JLabel("剩余数目");
    private JLabel price1 = new JLabel("价格");
    private JTextField textRid1 = new JTextField();
    private JTextField textAddress1 = new JTextField();
    private JTextField textCount1 = new JTextField();
    private JTextField textPrice1 = new JTextField();
    
    
    private JLabel tip21=new JLabel("以下是您的个人信息：");   //第二个选项卡
    private JLabel uid2 = new JLabel("用户账号");
    private JLabel name2 = new JLabel("姓名");
    private JLabel pass2 = new JLabel("密码");
    private JTextField textUid2 = new JTextField();
    private JTextField textName2  =new JTextField();  
    private JTextField textPass2  =new JTextField(); 
    private JLabel tip22=new JLabel("您可以修改自己的密码");
    private JLabel newPass1 = new JLabel("新密码");
    private JLabel newPass2 = new JLabel("确认密码");
    private JPasswordField textNew1 = new JPasswordField();
    private JPasswordField textNew2  =new JPasswordField(); 
    private JButton bConfirm2=new JButton("确定修改");
    
    private String[] recordTableHead= {"序号","姓名","房间序号","入住时间","退房时间"};  //第三个选项卡表头 
    private String[] roomTableHead = {"房间序号", "类型","所在位置","剩余数量","价格"};  //第四个选项卡表头
    
    
    private Font font = new Font("楷体", 0, 20);

    private JTabbedPane tabbedPane=new JTabbedPane(JTabbedPane.LEFT);
    
    private JButton bRefresh = new JButton("刷新");
    private JButton bExit = new JButton("退出");                    
    private JMenuBar menuBar = new JMenuBar();
    
    

	public UserUI(int uid) {
		this.uid=uid;
		setBounds((sc.width - 900) / 2, (sc.height - 500) / 2, 900, 500); 
		setResizable(false);
		tabbedPane.addTab("欢迎用户使用", card0);
		tabbedPane.addTab("1、预约服务", card1);
		tabbedPane.addTab("2、个人信息", card2);
		tabbedPane.addTab("3、租借记录", card3);
		tabbedPane.addTab("4、所有房间", card4);
		tabbedPane.setSelectedIndex(0);
		tabbedPane.setEnabledAt(0,false);//将0好选项卡设为不可选
		tabbedPane.setFont(font);
		

		
		tabbedPane.addChangeListener(new ChangeListener() {
		
			public void stateChanged(ChangeEvent e) {
				int selectIndex = tabbedPane.getSelectedIndex();  //找了几小时的错误（误把此句写到了方法外）
				if (selectIndex  == 1) {
                    initCard1();
                } else if (selectIndex ==2) {
                    initCard2();
                } else if (selectIndex == 3) {
                    initCard3();
                } else if (selectIndex == 4) {
                    initCard4();
                } 
			}
		});
		
		add(tabbedPane);
		setJMenuBar(menuBar);
		menuBar.add(bRefresh);
		menuBar.add(bExit);
		add(tabbedPane);
		
		//添加事件监听器
		
		bRefresh.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		UserUI.userShow(uid);
        		dispose();
        	}
        });
		bExit.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		setVisible(false);
        		Login.loginShow();
        	}
        });
		bQuery1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		String t=tType1.getText();
        		if(t.equals("")) {
        			ShowMessage.show("请先输入房间类型！");
        		}else {
        			Room r=roomDao.queryRoom(t);
        			if(r.getRid()==0) {                            //这里不能用if(r==null)，因为queryRoom决定了其永远不可能为空
        				ShowMessage.show("请输入有效房间名称！");
        			}else {
        			String s=Integer.toString(r.getRid());
        			String c=Integer.toString(r.getCount()-r.getHasRented());//求得剩余数并转化为字符型
        			String p=Integer.toString(r.getPrice());
        			textRid1.setText(s);
        			textAddress1.setText(r.getAddress());
        			textCount1.setText(c);
        			textPrice1.setText(p);
        			}
        			
        		}
        	}
        });
		bRemind1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(textRid1.getText().equals("")) {
        			ShowMessage.show("请先查找一个房间！");
        		}else {
        			int rid=Integer.parseInt(textRid1.getText());
        			remindDao.insertRemind(uid,rid);
        			ShowMessage.show("预约成功！\n如有房间空余将会在下次登陆时提醒");
        			
        		}
        			
        		}
		});
        	bConfirm2.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		String n1=new String(textNew1.getPassword());
        		String n2=new String(textNew2.getPassword());
        		if(n1.equals(n2)) {
        			userDao.updUserPassword(uid,n1);
        			ShowMessage.show("修改密码成功！");
        		}else {
        			ShowMessage.show("修改密码失败！\n两次密码输入不一致");
        		}
        	}
        });
		
		
		
	}
	
	private void initCard1() {
		 	type1.setBounds(150, 40, 80, 30);
		 	tType1.setBounds(230, 40, 200, 30);
		 	bQuery1.setBounds(450, 40, 100, 30);
	        rid1.setBounds(150, 100, 80, 30);
	        textRid1.setBounds(230, 100, 200, 30);
	        address1.setBounds(150, 160, 80, 30);
	        textAddress1.setBounds(230, 160, 200, 30);
	        count1.setBounds(150, 220, 80, 30);
	        textCount1.setBounds(230, 220, 200, 30);
	        price1.setBounds(150, 280, 80, 30);
	        textPrice1.setBounds(230, 280, 200, 30);
	        bRemind1.setBounds(220,350,200,50);
	        
	        type1.setFont(font);
	        bQuery1.setFont(font);
	        rid1.setFont(font);
	        address1.setFont(font);
	        count1.setFont(font);
	        price1.setFont(font);
	        bRemind1.setFont(font);
	        
	        
	        textRid1.setEditable(false);
	        textAddress1.setEditable(false);
	        textCount1.setEditable(false);
	        textPrice1.setEditable(false);
	        
	        card1.add(type1);
	        card1.add(tType1);
	        card1.add(bQuery1);
	        card1.add(rid1);
	        card1.add(textRid1);
	        card1.add(address1);
	        card1.add(textAddress1);
	        card1.add(count1);
	        card1.add(textCount1);
	        card1.add(price1);
	        card1.add(textPrice1);
	        card1.add(bRemind1);
	}
	
	private void initCard2() {
		
		tip21.setBounds(250, 40, 500, 30);
		uid2.setBounds(150, 80, 80, 30);
        textUid2.setBounds(230, 80, 200, 30);
        pass2.setBounds(150, 120, 80, 30);
        textPass2.setBounds(230, 120, 200, 30);
        name2.setBounds(150, 160, 80, 30);
        textName2.setBounds(230, 160, 200, 30);
        tip22.setBounds(200, 200, 200, 40);
        newPass1.setBounds(150, 250, 80, 30);
        textNew1.setBounds(230, 250, 200, 30);
        newPass2.setBounds(150, 290, 80, 30);
        textNew2.setBounds(230, 290, 200, 30);
        bConfirm2.setBounds(220, 350, 200, 45);
        
        textUid2.setText(Integer.toString(uid));
        textPass2.setText(userDao.queryUser(uid, null).getPassword());
        textName2.setText(userDao.queryUser(uid,null).getName());
         textUid2.setEditable(false);
         textPass2.setEditable(false);
         textName2.setEditable(false);
        
        tip21.setFont(font);
        uid2.setFont(font);
        pass2.setFont(font);
        name2.setFont(font);
        tip22.setFont(font);
        newPass1.setFont(font);
        newPass2.setFont(font);
        bConfirm2.setFont(font);
        
        
        card2.add(tip21);
        card2.add(uid2);
        card2.add(textUid2);
        card2.add(pass2);
        card2.add(textPass2);
        card2.add(name2);
        card2.add(textName2);
        card2.add(tip22);
        card2.add(newPass1);
        card2.add(textNew1);
        card2.add(newPass2);
        card2.add(textNew2);
        card2.add(bConfirm2);
    
    
		
	}
	
	private void initCard3() {
		JTable table1= new JTable(recordDao.getRecordsByUid(uid),recordTableHead);

		table1.setEnabled(false);
		table1.setFont(font);
		table1.setPreferredSize(new Dimension(720,450));
		
		table1.getTableHeader().setFont(font);  //分别对表体与表头进行大小和字体设计
		table1.getTableHeader().setPreferredSize(new Dimension(0,30));
		table1.setRowHeight(30);
		
		JScrollPane scroll = new JScrollPane(table1);
        scroll.setPreferredSize(new Dimension(720, 430));

        card3.add(scroll);

	}
	
	private void initCard4() {
		JTable table2= new JTable(roomDao.queryAllRoom(),roomTableHead);
		
		table2.setFont(font);
		table2.setPreferredSize(new Dimension(720,440));

		table2.getTableHeader().setFont(font);  //分别对表体与表头进行大小和字体设计
		table2.getTableHeader().setPreferredSize(new Dimension(0,30));
		table2.setEnabled(false);
		table2.setRowHeight(30);
		
		JScrollPane scroll = new JScrollPane(table2);
        scroll.setPreferredSize(new Dimension(720, 430));
     
        card4.add(scroll);

		
	}
	
	
	
	
	public static void userShow(int uid){
		JFrame u=new UserUI(uid);
		u.setVisible(true);
		u.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		u.setTitle("境外人员酒店隔离管理系统");
	}
}
